//
//  QDAboutViewController.swift
//  QDMetroClient
//
//  Created by YueWen on 2019/2/24.
//  Copyright © 2019 YueWen. All rights reserved.
//

import UIKit
import StoreKit // 每年有三次，并且获得不到次数

/// 青岛地铁关于界面
final class QDAboutViewController: NWTableViewController {
    
    /// 表头视图
    let headerView = QDAboutHeaderView()
    
    /// 底部展示青岛地铁的标签
    private let bottomLabel = UILabel()
    /// 底部展示copyRight的标签
    private let bottomSubLabel = UILabel()
    
    /// 暂时数据源
    var titles = ["官方客服电话","官方微博账号","官方微信账号","给我们评分"]
    ///
    var subtitles = ["0532-55770000","青岛地铁","青岛地铁",""]
    
    override func loadPropertysAtInitialization() {
        super.loadPropertysAtInitialization()
        
        headerRefreshEnable = false
        footerRefreshEnable = false
        tableView.rowHeight = 62
        tableViewConstraintInsets.bottom = 16.5 + 2.5 + 16 * 2 + BarDistance.TabBar.safeDistance
    }
    
    
    override func registerTableViewCellsAndReusableView() {
        tableView.register(QDTitleTableViewCell.self, forCellReuseIdentifier: "score")
        tableView.register(QDAboutActorTableViewCell.self, forCellReuseIdentifier: "action")
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.title = "关于"
        
        headerView.frame = CGRect(origin: .zero, size: CGSize(width: ritl_width, height: 205))
        tableView.tableHeaderView = headerView
        
        bottomLabel.text = "青岛地铁  版权所有"
        bottomLabel.textColor = QDColors.gray2
        bottomLabel.font = Font.regular.font(size: 11)
        
        bottomSubLabel.textColor = QDColors.gray2
        bottomSubLabel.font = Font.regular.font(size: 12)
        bottomSubLabel.text = "Copyright © 2018-\(DateFormatter.cachedDataFormatter(with: "YYYY").string(from: Date())).All Right Reserved"
        
        view.addSubview(bottomLabel)
        view.addSubview(bottomSubLabel)
        
        bottomSubLabel.snp.makeConstraints { (make) in
            make.height.equalTo(16)
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().inset((hasPhoneHeader() ? 0 : 16.5) + BarDistance.TabBar.safeDistance)
        }
        
        bottomLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.height.equalTo(16)
            make.bottom.equalTo(bottomSubLabel.snp.top).offset(3)
        }
        
    }

    // MARK: navigationBar
    override var nav_barShadowHidden: Bool { return false }
}



//TODO: TableViewDelegate、TableViewDatasource
extension QDAboutViewController {
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return titles.count
    }
    
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: indexPath.item == titles.count - 1 ? "score" : "action", for: indexPath)
        
        /// 单纯的文字标题
        if let titleCell = cell as? QDTitleTableViewCell {
            titleCell.titleLabel.text = titles[indexPath.row]
        }
        
        /// 后面可以点击响应
        else if let actorCell = cell as? QDAboutActorTableViewCell {
            actorCell.titleLabel.text = titles[indexPath.row]
            actorCell.actionButton.setTitle(subtitles[indexPath.row], for: .normal)
            // 响应
            actorCell.actionHandler = {
                
                if indexPath.row == 0 {
                    RITLCall("053255770000")//拨打地铁客服
                }else if (indexPath.row == 1 || indexPath.row == 2) {
                    UIPasteboard.general.string = "青岛地铁"
                    self.view.showTextHUD(text: "复制成功")
                }
            }
        }
        
        return cell
        
    }
    
    
    
    //点击时间
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        /// 表示打分
        if indexPath.row == titles.count - 1 {
            goToStoreReview()
        }
        
    }
}




extension QDAboutViewController {
    
    /// 进行打分
    fileprivate func goToStoreReview(){
        
        guard let url = URL(string: "https://itunes.apple.com/cn/app//id1373547573?l=zh&ls=1&mt=8?action=write-review") else {
            return
        }
        
        if UIApplication.shared.canOpenURL(url){
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(url, options: [:], completionHandler: nil)
            } else {
                // Fallback on earlier versions
                UIApplication.shared.openURL(url)
            }
        }
    }
}

